layui.define(['table', 'form', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    //
    table.render({
        elem: '#LAY-user-manage'
        , url: '/agent/user/getlist'
        , cols: [[
            {field: 'id', width: 100, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'account', title: '账号', width: 140}
            , {
                field: '', title: '真实姓名', width: 100, templet: function (d) {
                    return d.anchor_auth ? d.anchor_auth.real_name : '';
                }
            }
            , {
                field: 'nick_name', title: '昵称', width: 130, templet: function (d) {
                    return decodeURI(d.nick_name);
                }
            }
            , {
                field: '', title: '头像', width: 70, templet: function (d) {
                    if (d.avatar) {
                        return '<img style="display: inline-block; height: 100%;" src= "' + d.avatar + '">'
                    } else {
                        return "无";
                    }
                }
            }
            , {field: 'anchor_level', title: '主播星级', width: 90}
            , {
                field: 'gender', width: 70, title: '性别', templet: function (d) {
                    if (!d.profile) {
                        return '未知';
                    }
                    switch (d.profile.gender) {
                        case 1:
                            return '男';
                        case 2:
                            return '女';
                        default:
                            return "未知";
                    }
                }
            }
            , {field: 'diamond', width: 90, title: '宝石'}
            , {
                field: '', width: 90, title: '充值金额', templet: function (d) {
                    return d.order_info ? d.order_info.pay_amount : 0;
                }
            }
            , {
                field: 'online_status', width: 90, title: '在线状态', templet: function (d) {
                    switch (d.online_status) {
                        case 1:
                            return '<span class="layui-badge layui-bg-green">在线</span>';
                        case 2:
                            return '<span class="layui-badge layui-bg-orange">暂离</span>';
                        case 3:
                            return '<span class="layui-badge layui-bg-red">通话中</span>';
                        case 9:
                            return '<span class="layui-badge layui-bg-black">离线</span>';
                        default:
                            return '<span class="layui-badge layui-bg-gray">未知</span>';
                    }
                }
            }
            , {
                field: '', width: 90, title: '分成比例', templet: function (d) {
                    return d.sharing_ratio + "%";
                }
            }
            , {field: 'regist_time', title: '注册时间'}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-userlist-operation'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-180'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-user-manage)', function (obj) {
        if (obj.event === 'edit') {
            layer.prompt({
                formType: 0,
                value: obj.data.sharing_ratio,
                title: '分成比例',
            }, function (value, index, elem) {
                admin.req({
                    url: '/agent/user/edit_ratio'
                    , data: {'uid': obj.data.id, 'sharing_ratio': value}
                    , success: function (res) {
                        if (res.code == 0) {
                            layer.close(index);
                            layer.msg(res.msg, {icon: 1});
                            table.reload('LAY-user-manage');
                        }
                    }
                });
            });
        }
    });

    exports('agent/user', {})
});